package com.data.modules.bpm.controller;

import com.data.common.api.vo.Result;
import com.data.modules.bpm.service.IFlowSerive;
import com.data.modules.bpm.service.IProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.impl.persistence.deploy.cache.DeploymentCache;
import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Description: 系统logo相关表
* @Author: data-boot
* @Date:   2022-05-25
* @Version: V1.0
*/
@SuppressWarnings("ALL")
@Api("BPM")
@RestController
@RequestMapping("/BPM/")
@Slf4j
public class BpmController {
    
    @Autowired
    private IFlowSerive flow;
    @Autowired
    private IProcessService process;
    // 使用ProcessEngineConfiguration获取缓存并清理
    @Autowired
    private SpringProcessEngineConfiguration springProcessEngineConfiguration;
    
    
    @ApiOperation(value="cacheClear", notes="cacheClear")
    @GetMapping("/cacheClear")
    public Result<?> cacheClear(String deploymentId){
        //按 部署的流程ID 清理其缓存
        //需要将 缓存 调整至redis便于 集群、分布式管理缓存
        DeploymentCache cache = springProcessEngineConfiguration
                .getDeploymentCache();
        cache.removeDeployment(deploymentId);
        //清理 BPM 全部缓存
        //springProcessEngineConfiguration.getDeploymentCache().purgeCache();
        return Result.OK();
    }
    @ApiOperation(value="部署流程", notes="部署流程")
    @GetMapping("/deployProcess")
    public Result<?> deployProcess(String proName,String proPath){
        process.deployProcess(proName,proPath);
        return Result.OK();
    }
    
    @ApiOperation(value="销毁流程", notes="销毁流程")
    @GetMapping("/deployLeaveProcess")
    public Result<?> deployLeaveProcess(String proDefId){
        process.destroyProces(proDefId);
        return Result.OK();
    }
    
    @ApiOperation(value="start流程实例", notes="tart流程实例")
    @GetMapping("/start")
    public Result<?> start(String bizKey){
        flow.startProcess(bizKey);
        
        return Result.OK();
    }
    @ApiOperation(value="apply流程实例", notes="apply流程实例")
    @GetMapping("/apply")
    public Result<?> apply(String bizKey){
        log.debug("bizKey:{}",bizKey);
        flow.apply(bizKey);
        return Result.OK();
    }
    
    @ApiOperation(value="reject流程实例", notes="reject流程实例")
    @GetMapping("/reject")
    public Result<?> reject(){
        //process.deployLeaveProcess();
        return Result.OK();
    }
    
    

}
